! mixed_fortran_c.f90
program main
    use mpi
    use, intrinsic :: iso_c_binding
    implicit none

    interface
        subroutine allreduce_c(sendbuf, recvbuf, count) bind(C, name="allreduce_c")
            import :: c_double, c_int
            real(c_double), intent(in)  :: sendbuf(*)
            real(c_double), intent(out) :: recvbuf(*)
            integer(c_int), intent(in)  :: count
        end subroutine allreduce_c
    end interface

    integer :: ierr, rank, size
    integer :: comm_world
    integer(c_int) :: count
    real(c_double) :: sendbuf(2), recvbuf(2)

    call MPI_Init(ierr)
    comm_world = MPI_COMM_WORLD

    call MPI_Comm_rank(comm_world, rank, ierr)
    call MPI_Comm_size(comm_world, size, ierr)


    count = 2
    sendbuf = [1.0d0 + rank, 10.0d0 + rank]
    recvbuf = 0.0d0

    ! 调用 C 实现的 allreduce
    call allreduce_c(sendbuf, recvbuf, count)

    print *, "Rank", rank, ": Results =", recvbuf

    call MPI_Finalize(ierr)
end program main
